//
//  MCHomePageLotteryCategoriesView.m
//  Uhome
//
//  Created by miaocai on 2017/5/27.
//  Copyright © 2017年 menhao. All rights reserved.
//

#import "MCHomePageLotteryCategoriesView.h"
#import "MCHomePageLotteryCategoriesCollectionViewCell.h"
#import "LXReorderableCollectionViewFlowLayout.h"
#import "MCUserDefinedLotteryCategoriesViewController.h"
#import "MCUserDefinedLotteryCategoriesModel.h"
#import "MCDataTool.h"

@interface MCHomePageLotteryCategoriesView()<UICollectionViewDelegate,LXReorderableCollectionViewDataSource>
#pragma mark - property
//根view
@property (nonatomic,weak) UIView *contentView;
//我的彩种
@property (nonatomic,weak) UILabel *lotteryCategoriesLabel;
//自定义按钮
@property (nonatomic,weak) UIButton *customButton;
//分割线
@property (nonatomic,weak) UIView *separatorView;
//collectionView
@property (nonatomic,weak) UICollectionView *collectionView;
//数据源
@property (nonatomic,strong) NSMutableArray * dataMarry;

@end

@implementation MCHomePageLotteryCategoriesView

static NSString *const HomePageLotteryCategoriesID = @"MCHomePageLotteryCategoriesView";

#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setUpUI];
    }
    return self;
}



- (void)setUpUI{
    
    self.dataMarry=[MCDataTool MC_GetMarr_withID:MCHomePageLotteryCategoryData];
    //根view
    UIView *contentView = [[UIView alloc] init];
    self.contentView = contentView;
    [self addSubview:contentView];
    contentView.backgroundColor = [UIColor whiteColor];
    
    
    //我的彩种
    UILabel *lotteryCategoriesLabel = [[UILabel alloc] init];
    self.lotteryCategoriesLabel = lotteryCategoriesLabel;
    [contentView addSubview:lotteryCategoriesLabel];
    lotteryCategoriesLabel.text = @"我的彩种";
    lotteryCategoriesLabel.textColor = MC_ColorWithAlpha(119, 119, 119, 1);
    lotteryCategoriesLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(15)];
    
    
    //自定义按钮
    UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.customButton = customButton;
    [contentView addSubview:customButton];
    [customButton setTitle:@"自定义" forState:UIControlStateNormal];
    customButton.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(15)];
    [customButton setTitleColor:MC_ColorWithAlpha(54, 128, 211, 1) forState:UIControlStateNormal];
    [customButton addTarget:self action:@selector(customLotteryCategoriesButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //分割线
    UIView *separatorView = [[UIView alloc] init];
    [self addSubview:separatorView];
    separatorView.backgroundColor = RGB(218, 218, 218);
    self.separatorView = separatorView;
    
    
    //collectionView
    CGFloat padding = MC_REALVALUE(30);
    LXReorderableCollectionViewFlowLayout *layout = [[LXReorderableCollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((G_SCREENWIDTH - padding * 2 - 20)/3, (G_SCREENWIDTH - padding * 3- 20)/3);
    layout.sectionInset = UIEdgeInsetsMake(padding, 0, 0, 0);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView = collectionView;
    [contentView addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MCHomePageLotteryCategoriesCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:HomePageLotteryCategoriesID];
    collectionView.backgroundColor = RGB(253, 253, 253);
    collectionView.contentSize = CGSizeMake(G_SCREENWIDTH -20, G_SCREENWIDTH);
}

#pragma mark - Layout
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat padding = MC_REALVALUE(5);
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.lotteryCategoriesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(padding);
        make.top.equalTo(self.contentView.mas_top).offset(padding);
        make.height.equalTo(@(MC_REALVALUE(30)));
        
    }];
    
    [self.customButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lotteryCategoriesLabel.mas_top);
        make.right.equalTo(self.contentView.mas_right).offset(-padding);
        make.centerY.equalTo(self.lotteryCategoriesLabel.mas_centerY);
    }];
    
    [self.separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@0.5);
        make.top.equalTo(self.lotteryCategoriesLabel.mas_bottom);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.separatorView.mas_bottom);
        make.left.equalTo(self.contentView.mas_left).offset(2 * padding);
        make.right.equalTo(self.contentView.mas_right).offset(-2 * padding);
        make.height.equalTo(@((G_SCREENHEIGHT - MC_REALVALUE(260) - 49 - 64)));
    }];
}

#pragma mark - collectionViewDelegate And UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataMarry.count + 1;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MCHomePageLotteryCategoriesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HomePageLotteryCategoriesID forIndexPath:indexPath];
    
    if (indexPath.row == self.dataMarry.count) {
        
        MCUserDefinedLotteryCategoriesModel * model=[[MCUserDefinedLotteryCategoriesModel alloc]init];
        model.logo=@"添加";
        model.name=@"";
        cell.dataSource=model;
        cell.iconImageV.backgroundColor=RGB(224, 224, 224);
        
    } else {
        MCUserDefinedLotteryCategoriesModel * model=[self.dataMarry objectAtIndex:indexPath.row];
        cell.dataSource=model;
    }
    return  cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == self.dataMarry.count) {
        
        /*
         * 跳转自定义彩种
         */
        
        if ([self.delegate respondsToSelector:@selector(SetUserDefinedLotteryCategories)]) {
            [self.delegate SetUserDefinedLotteryCategories];
        }
        
    } else {
        MCUserDefinedLotteryCategoriesModel * model=[self.dataMarry objectAtIndex:indexPath.row];
        if (self.collectionViewDidSelectedCallBack != nil) {
            
            self.collectionViewDidSelectedCallBack(model);
        }
    }
    
    
}
- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath willMoveToIndexPath:(NSIndexPath *)toIndexPath {
    id object = [self.dataMarry objectAtIndex:fromIndexPath.item];
    [self.dataMarry removeObjectAtIndex:fromIndexPath.item];
    [self.dataMarry insertObject:object atIndex:toIndexPath.item];
}
#pragma mark - Datas parse
- (void)customLotteryCategoriesButtonClick:(UIButton *)btn{
    /*
     * 跳转自定义彩种
     */
    
    if ([self.delegate respondsToSelector:@selector(SetUserDefinedLotteryCategories)]) {
        [self.delegate SetUserDefinedLotteryCategories];
    }
    
}

#pragma mark-刷新数据
-(void)relayOutUI{
    
    [self.dataMarry removeAllObjects];
    self.dataMarry=[MCDataTool MC_GetMarr_withID:MCHomePageLotteryCategoryData];
    [self.collectionView reloadData];
    
}

@end







